﻿using DemoUser.Application.User.Interface;
using DemoUser.Core.User;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoUser.Application.User
{
    public class UserAppService:IDynamicApiController
    {

        private readonly IUserService _userService;

        public UserAppService(IUserService userService)
        {
            _userService = userService;
        }


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public bool Login(string username,string password)
        {
            OUser user = _userService.SelectByUsername(username);
            if (user != null)
            {
                Console.WriteLine("数据库有此用户");
                if (password.Equals(user.Password))
                {
                    return true;
                }
            }
            else
            {
                Console.WriteLine("数据库没有此用户,需要注册");
                return false;
            }
            return false;
        }

        /// <summary>
        /// 获取Token
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpGet]
        public string GetToken(string username, string password)
        {
            return _userService.GenerateToken(username,password);
        }
        
        //注册
    }
}
